package evolution.random;

public class SFMT19937RandomGen implements RandomGenerator {

	private SFMT19937 random = new SFMT19937();
	private static final long maxInt = Integer.MAX_VALUE;

	@Override
	public double nextDouble() {
		int randomInt = random.next();
		return scale(randomInt, 1);
	}

	@Override
	public int nextInt(int n) {
		int randomInt = random.next();
		return (int) scale(randomInt, n);
	}

	/**
	 * Scale integer to [0, max).
	 * 
	 * @param val
	 * @param max
	 * @return
	 */
	public static double scale(long val, int max) {
		val -= Integer.MIN_VALUE;
		double result = max * val / (maxInt * 2.0 + 1);
		return result;
	}
}
